<?php
namespace Manager\Controller;
use Think\Controller;
/**
* ==============================================
* URL：http://www.liuhuanhui.cn
* ==============================================
* @date：2014-11-5
* @time：下午4:42:23
* @author：liuhuanhui
* @encoding：UTF-8
*/
class ControlsController extends Controller
{
	/**
	 * 显示控件列表
	 */
	function index()
	{
		$this->controls=M('controls')->select();
		$this->display();
	}
	/**
	 * 显示前台控件
	 */
	function view()
	{	
		$where['page']='Index';
		if(!empty($_POST['page']))
			$where['page']=I('page');
		$where['type']=array('neq','CHILD');
		$this->controls=M('pagecontrol')->where($where)->order('number desc')->select();
		$this->display();
	}
	/**
	 *删除
	 */
	
	function delete()
	{
		if(M('pagecontrol')->delete(I('id')))
			$this->success('删除页面控件成功！',U('view'));
		else
			$this->error('删除页面控件失败！');
	}
	/**
	 * 可见
	 */
	function visible()
	{
		if(M('pagecontrol')->where('id='.I('id'))->setField('visible',I('visible')))
			$this->success('改变控件状态成功！',U('view'));
		else
			$this->error('改变控件状态失败！');
	}
	/**
	 * 添加页面控件
	 */
	function create()
	{
		$this->controls=M('controls')->select();
		$this->display();
	}
	/**
	 * 添加处理方法
	 */
	function createHandle()
	{
		/*
		$kv = new \SaeKV();  //创建SaeKV对象
		$ret = $kv->init(); //初始化
		$ret = $kv->pkrget('', 100);    //获取缓存内容(帮助文档注明了上限为100条)
		$this->show('<h3>当前KVDB缓存内容</h3>');
		dump($ret);
		//遍历并删除
		foreach ($ret as $key => $value) {
			$ret = $kv->delete($key);
		}
		$this->show('<hr>');
		$this->show('<h3>清空KVDB缓存后的内容</h3>');
		$ret = $kv->pkrget('', 100);
		dump($ret);
		if (!$ret){
			$this->show('<hr>');
			$this->show('KVDB清空成功');
		}; 
	*/
		$pageControl=M('pagecontrol');
		$controls=I('controlId');
		if(count($controls)>1)
		{
			$data=array(
				type=>'TAB',
				title=>I('title'),
				page=>I('page'),
			    count=>I('count'),
				number=>I('number'),
				visible=>I('visible',"y"),
				control_id=>$controls[0],
			);
			$id=$pageControl->add($data);
			$datas=array();
			foreach($controls as $v)
			{
				$datas[]=array(
					type=>'CHILD',
					pid=>$id,
					number=>I('number'),
					page=>I('page'),
					control_id=>$v,
				);
			}
			if($pageControl->addAll($datas))
				$this->success('页面添加新TAB控件成功！',U('view'));
			else
				$this->error('页面添加控件失败！');
		}else{
			$data=array(
				type=>'UL',
				title=>I('title'),
				page=>I('page'),
			    count=>I('count'),
					number=>I('number'),
				visible=>I('visible',"y"),
				control_id=>$controls[0],
			);
			if($pageControl->add($data)){
				$this->success('页面添加新UL控件成功！',U('view'));
            }
			else
				$this->error('页面添加控件失败！');
		}
	}
}
?>